<?php

function showFlickrPhotos($target){
	global $blogURL, $pluginURL, $configVal;
	requireComponent('Textcube.Function.misc');
	require_once "lib/phpFlickr.php";
	
	$data = misc::fetchConfigVal($configVal);
	$flickruserid = isset($data['flickruserid']) ? $data['flickruserid'] : "";
	
	$flickr_api_key = "d1038f051000214af2bf694014ca8f98"; //It's key for plugin. It does not change.
	$f = new phpFlickr($flickr_api_key);

	ob_start();
?>
	<script language="javascript" type="text/javascript">
	//<![CDDA[
		function actionFlickr(flag){
			var person = document.getElementById("person").value;
			var photoSets = (document.getElementById("photoSets")==undefined) ? "" : document.getElementById("photoSets").value;
			var length = document.getElementById("length").value;
			var searchKey = document.getElementById("searchKey").value;
			var srcTarget = document.getElementById("flickrview");

			switch(flag) {
				case "search":
					var flickrQueryString = "?person=" + person + "&photoSets=" + photoSets + "&length=" + length + "&search=" + encodeURIComponent(searchKey);
					srcTarget.src = "<?php echo $blogURL;?>/plugin/flickrPhotos" + flickrQueryString;
				break;
				case "allview":
					var flickrQueryString = "?person=" + person + "&length=" + length;
					srcTarget.src = "<?php echo $blogURL;?>/plugin/flickrPhotos" + flickrQueryString;
				break;
				case "refresh":
				default:
					parent.frames['flickrview'].location.reload();
				break;
			}
		}
	//]]>
	</script>
	<div style="margin-top:10px;clear:both;"><strong>&bull; Flickr Photos</strong></div>
	<?php
	if(!empty($flickruserid)) {
		?>
		<div id="photosToyHeader" style="margin-top:15px;background-color:#eee;">
		<?php
		$userInfo = $f->people_getInfo($flickruserid);
		echo '&nbsp;Hello!&nbsp;<strong style="color: #36f;">',$userInfo['username'],'</strong>';
		$photosUrl = $userInfo['photosurl'];
		?>
		</div>
		<?php
	}
	?>	
	<div style="margin-top:5px;background-color:#fff;"><iframe name="flickrview" id="flickrview" src="<?php echo $blogURL;?>/plugin/flickrPhotos" scrolling="no" style="width:100%;height:200px;margin:0;padding:0;border:0;"></iframe></div>
	<div id="photosToolbar" style="background-color:#eee;white-space:nowrap;padding:4px;overflow:hidden;">
	<?php
	if(!empty($flickruserid)) {
		$mySets = $f->photosets_getList($flickruserid);
		echo 'Flickr Photosets&nbsp;<select id="photoSets" name="photoSets" size="1">';
		foreach((array)$mySets['photoset'] as $row) {
			echo '<option label="',$row['title'],'" value="',$row['id'],'">',$row['title'],'(',$row['photos'],')</option>';
		}
		echo '</select><br /><br />';
	}
	?>
	Search&nbsp;<select name="person" id="person" value="1"><option label="My" value="my">My</option><option label="All" value="all">All</option></select>&nbsp;<input type="text" name="searchKey" id="searchKey" value="" maxlength="255" />&nbsp;&nbsp;
	Lists&nbsp;<select name="length" id="length" value="1"><option label="10" value="10">10</option><option label="20" value="20">20</option><option label="30" value="30">30</option><option label="40" value="40">40</option><option label="50" value="50">50</option></select><br /><br />
	<img src="<?php echo $pluginURL;?>/images/refresh.gif" width="82" height="16" border="0" alt="" style="cursor:pointer;" onclick="actionFlickr('refresh');return false;" />&nbsp;<img src="<?php echo $pluginURL;?>/images/search.gif" width="82" height="16" border="0" alt="" style="cursor:pointer;" onclick="actionFlickr('search');return false;" />&nbsp;<img src="<?php echo $pluginURL;?>/images/allview.gif" width="82" height="16" border="0" alt="" style="cursor:pointer;" onclick="actionFlickr('allview');return false;" />
	</div>
	<?php
	
	$script = ob_get_contents();
	ob_end_clean();
	return $target.$script;
}
function getFlickrPhotosInfo($photo = array(), $zoomstyle = 'flickr', $imgSizeInfo = array()) {
	foreach((array)$imgSizeInfo as $_info) {
		switch($_info['label']) {
			case "Square":
				$imgSrc = $_info['source'];
			break;
			case "Large":
				$imgLarge = $_info['source'];
			break;
			case "Original":
				$imgOriginal = $_info['source'];
			break;
			case "Medium":
			default:
				$imgLink = str_replace("/sizes/m/","",$_info['url']);
				$imgMedium = $_info['source'];
		}
	}
	$imgLightBoxLarge = (isset($imgLarge)) ? $imgLarge : $imgOriginal;
	$_output = "<a href='" . $imgLink . "' title='{$photo['title']}' onclick=\"insertPhoto('{$imgLink}', '" . $imgMedium . "', '" . $imgLightBoxLarge . "', '{$photo['title']}', '{$zoomstyle}'); return false;\" >";
	$_output .= "<img src='" . $imgSrc . "' width='75' height='75' border='0' alt='{$photo['title']}'>";
	$_output .= "</a>";

	return $_output;
}
function getFlickrPhotos($target) {
	global $pluginURL, $configVal;
	requireComponent('Textcube.Function.misc');
	require_once "lib/phpFlickr.php";

	$data = misc::fetchConfigVal($configVal);
	$flickruserid = isset($data['flickruserid']) ? $data['flickruserid'] : "";
	
	$flickr_api_key = "d1038f051000214af2bf694014ca8f98"; //It's key for plugin. It does not change.
	$f = new phpFlickr($flickr_api_key);

	$listLength = isset($_GET['length']) ? $_GET['length'] : 10;
	$searchKey	= (isset($_GET['search']) && !empty($_GET['search'])) ? urldecode($_GET['search']) : "";
	$photoSets	= (isset($_GET['photoSets']) && !empty($_GET['photoSets'])) ? $_GET['photoSets'] : "";
	$personMode = isset($_GET['person']) ? $_GET['person'] : "my";
	$zoomStyle = "flickr";
	?>
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<style type="text/css">
	<!--
	body{ font-size: 0.82em; }
	a{ margin-right:1px; }
	-->
	</style>
	<script type="text/javascript">
	//<![CDDA[
		function insertPhoto(_link, _src_m, _src_l, _title, _style){
			var linkTag_A = '', linkTag_B = '', imgTag = '';
			linkTag_A = '<a href="' + _link + '" title="' + _title + '" target="_blank">'; 
			linkTag_B = '</a>';
			imgTag	= '<img src="' + _src_m + '" border="0" />';

			var isWYSIWYG = false;
			try{
				if(parent.editor.editMode == 'WYSIWYG')
					isWYSIWYG = true;
			}
			catch(e){ }
			if(isWYSIWYG) {
				parent.editor.command('Raw', (linkTag_A + imgTag), linkTag_B);
			}else{
				parent.insertTag(parent.editor.textarea, (linkTag_A + imgTag), linkTag_B);
			}
		}
	//]]>
	</script>	
	</head>
	<body style="margin:0;padding:0;border:none;background-color:#ffffff;">
	<div id="photosBody" style="height:200px;width:100%;overflow-x:hidden;overflow-y:auto;background-color:#fff;">
	<?php	
	if ($personMode == "my" && !empty($flickruserid) && empty($searchKey)) {
		$photos_url = $f->urls_getUserPhotos($flickruserid);
		if(!empty($photoSets)) {
			$photos = $f->photosets_getPhotos($photoSets, NULL, NULL, $listLength);
		} else {
			$photos = $f->people_getPublicPhotos($flickruserid, NULL, $listLength);
		}
		foreach ((array)$photos['photo'] as $photo) {
			$imgSizeInfo = $f->photos_getSizes($photo['id']);
			$_output = getFlickrPhotosInfo($photo, $zoomStyle, $imgSizeInfo);
			echo $_output;
		}
	} else if (($personMode == "my" || $personMode == "all") && !empty($searchKey)){
		$searchUser = "";
		if($personMode == "my" && !empty($flickruserid)){
			$searchUser = array("user_id"=>$flickruserid);
		}
		$searchArray = array($searchUser,"text"=>$searchKey, "sort"=>"date-posted-desc", "per_page"=>$listLength);
		$photos_cat = $f->photos_search($searchArray);
		if (count((array)$photos_cat['photo']) > 0) {
			foreach ((array)$photos_cat['photo'] as $photo) {
				$imgSizeInfo = $f->photos_getSizes($photo['id']);
				$_output = getFlickrPhotosInfo($photo, $zoomStyle, $imgSizeInfo);
				echo $_output;
			}	
		} else {
			echo '<p style="margin-top:30px; text-align:center; font-size:9pt;">Flickr did not find any photos. Please input title, description or tags.</p>';		
		}
	} else if ($personMode == "all" && empty($searchKey)){
		$photos_cat = $f->photos_getRecent(NULL,$listLength ,NULL);

		foreach ((array)$photos_cat['photo'] as $photo) {
			$imgSizeInfo = $f->photos_getSizes($photo['id']);
			$_output = getFlickrPhotosInfo($photo, $zoomStyle, $imgSizeInfo);
			echo $_output;
		}		
	} else {
		echo '<p style="margin-top:30px; text-align:center; font-size:9pt;">Flickr did not find any photos.' , (!$nickName ? '(Input title, description or tags.)' : '') , '</p>';
	}
	?>
	</div>
	</body>
	</html>
	<?php		
}

function getFlickrPhotosDataSet($DATA) {
	requireComponent('Textcube.Function.misc');
	$cfg = misc::fetchConfigVal($DATA);
	if(!$cfg['flickruserid'] || empty($cfg['flickruserid'])) return "::Input error::\n\n Flickr's my flickr user ID must input certainly.  ";
	return true;
}
?>